package state

import "fmt"

type State interface {
	On(m *Machine)
	Off(m *Machine)
}

type Machine struct {
	current State
}

func NewMachine() *Machine {
	return &Machine{NewOff()}
}

func (m *Machine) setCurrent(s State) {
	m.current = s
}

func (m *Machine) On() {
	m.current.On(m)
}

func (m *Machine) Off() {
	m.current.Off(m)
}

type On struct{}

func NewOn() State {
	return &On{}
}

func (o *On) On(m *Machine) {
	fmt.Println("The machine is already on")
}

func (o *On) Off(m *Machine) {
	fmt.Println("Turning the machine off")
	m.setCurrent(NewOff())
}

type Off struct{}

func NewOff() State {
	return &Off{}
}

func (o *Off) On(m *Machine) {
	fmt.Println("Turning the machine on")
	m.setCurrent(NewOn())
}

func (o *Off) Off(m *Machine) {
	fmt.Println("The machine is already off")
}
